package com.zx.练习题.demo;

import java.util.ArrayList;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

@Slf4j
public class Demo {

    static int counter = 0;

    // 给一个正整数，判断是否是2的n次幂
    public static void main(String[] args) {
        log.info("{} = {}", 2, isN(2));
        log.info("{} = {}", 3, isN(3));
        log.info("{} = {}", 4, isN(4));
        log.info("{} = {}", 5, isN(5));
        log.info("{} = {}", 6, isN(6));
        log.info("{} = {}", 7, isN(7));
        log.info("{} = {}", 8, isN(8));
        log.info("{} = {}", 9, isN(9));
    }

    public static boolean isN(int n) {
        // 是2的倍数的话, 下面的操作后应该为0
        int t = (n & (n - 1));
        return t == 0;
    }

    /**
     * 基因法-分库分表
     */
    @Test
    public void tmp1() {
        for (int i = 100; i < 10000; i++) {
            tmp1(i);
        }
        System.out.println(counter);
    }

    public void tmp1(int num) {
        List<Integer> ins = new ArrayList<>();
        int cifang = 1;
        int n = 2;

        while (true) {
            counter++;
            // num 取 n的余数
            int tmp = num % n;
            // num 最后 n 位
            int modulo = num & ((1 << cifang) - 1);
            if (tmp != modulo) {
                log.info("理论错误, num={}, cifang={}, tmp={}, modulo={}", num, cifang, tmp, modulo);
                return;
            }
            cifang++;
            n = n << 1;
            if (n > num) {
                break;
            }
        }
    }

}
